package com.sky.controller.admin;/*
@author sun
@date 24/07/15
@project IntelliJ IDEA
*/

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 分类管理
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    // 新增分类
    @PostMapping
    public Result<String> addOneCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.addOneCategory(categoryDTO);
        return Result.success();
    }

    // 分类分页查询
    @GetMapping("/page")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    // 根据id删除分类
    @DeleteMapping
    public Result<String> delCategoryById(Long id){
        categoryService.delCategoryById(id);
        return Result.success();
    }
    // 修改分类
    @PutMapping
    public Result<String> updateCategory (@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }
    // 启用、禁用分类
    @PostMapping("/status/{status}")
    public Result<String> startOrStop (@PathVariable("status") Integer status,Long id){
        categoryService.startOrStop(status,id);
        return Result.success();
    }

    // 根据类型查询分类
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.queryByType(type);
        return Result.success(list);
    }

}
